package com.tg.app.camera;

import androidx.annotation.NonNull;

import com.appbase.custom.constant.CommonConstants;
import com.tange.core.backend.service.http.ClientObserver;
import com.tange.module.camera.hub.CameraHub;
import com.tg.app.util.LogUtils;
import com.tg.data.bean.DeviceItem;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.data.http.entity.EmptyBean;
import com.tg.network.socket.http.TGHttp;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class P2PReport {
    private static final String REPORT_EVENT = "iCamera_p2p";
    private static final String REPORT_SUB_EVENT = "first_avframe";
    public static final String DEVICE_LIST_PAGE = "home_page";
    public static final String MESSAGE_DETAIL_PAGE = "message_detail_page";
    public static final String DEVICE_SCARD_PAGE = "device_scard_page";

    public static long startTime = 0;
    public static long data_bytes = 0;
    public static boolean isPlayerStart = false;
    public static long duration = 0;
    public static int playType = 0;

    public static void reportFirstAVFrame(@NonNull  Camera camera, @NonNull DeviceSettingsInfo info, @NonNull DeviceItem deviceItem, String playType, String from){
        long times = System.currentTimeMillis() - camera.getClickTime();

        HashMap<String,String> map = new HashMap<>();
        map.put("event", REPORT_EVENT);
        map.put("title", REPORT_SUB_EVENT);
        map.put("times", Long.toString(times));
        map.put("Mode", camera.mode);
        map.put("p2p_type", camera.getTag());
        map.put("preconnect", String.valueOf(camera.getPreConnect()));
        map.put("play_type", playType);
        map.put("device_type", deviceItem.device_type);
        map.put("version", info.version);
        map.put("from", from);
        map.put("RemoteIP", camera.getRemoteIp());
        map.put("RemotePort", String.valueOf(camera.getRemotePort()));
        map.put("bEnableLanSearch", String.valueOf(camera.getEnableLanSearch()));
        map.put("level","NORMAL");

        Camera cameraInstance = CameraHub.getInstance().getCamera(info.uuid);
        if (cameraInstance != null) {
            map.put("uuid", cameraInstance.uid);
            map.put("p2pid", cameraInstance.p2pid);
            map.put("sessionid", String.valueOf(cameraInstance.sessionID));
        }

        LogUtils.uploadLog(map);
    }

    public static void reportMediaCodecInfo(String mime, int width, int height, int nCodeId, boolean isCarDevice, boolean surfaceNull){
        HashMap<String,String> map = new HashMap<>();
        map.put("event", "mediaCodec_Info");
        map.put("title", "video_format");
        map.put("mime", mime);
        map.put("width", String.valueOf(width));
        map.put("height", String.valueOf(height));
        map.put("code_id", String.valueOf(nCodeId));
        map.put("isCarDevice", isCarDevice? "1" : "0");
        map.put("surface_null", surfaceNull? "1" : "0");
        map.put("level","NORMAL");
        LogUtils.uploadLog(map);
    }

    public static void reportCameraStatusOfClickBtn(String uuid, String btnName, String from, int status){
        HashMap<String,String> map = new HashMap<>();
        map.put("event", REPORT_EVENT);
        map.put("title", REPORT_SUB_EVENT);
        map.put("btnName", btnName);
        map.put("from", from);
        map.put("uuid", uuid);
        map.put("status", String.valueOf(status));
        map.put("level","NORMAL");
        LogUtils.uploadLog(map);
    }

    public static void initPlayerInfo(){
        P2PReport.startTime = System.currentTimeMillis() / 1000;
        P2PReport.isPlayerStart = false;
        P2PReport.duration = 0;
        P2PReport.data_bytes = 0;
    }

    public static void reportPlayInfo(String uuid, String p2pMode){
        if (P2PReport.isPlayerStart){
            String type;
            if (P2PReport.playType == CommonConstants.CAMERA_TYPE_LIVE){
                type = "realtime";
            }else if (P2PReport.playType == CommonConstants.CAMERA_TYPE_PLAYBACK_SDCARD){
                type = "sdcard";
            }else{
                type = "cloud";
            }

            HashMap<String, Object> map = new HashMap<>();
            map.put("uuid", uuid);
            map.put("start_time", String.valueOf(P2PReport.startTime));
            map.put("duration", String.valueOf(P2PReport.duration));
            map.put("data_bytes", String.valueOf(P2PReport.data_bytes));
            map.put("p2p_mode", p2pMode);
            map.put("type", type);
            P2PReport.isPlayerStart = false;
            TGHttp.getInstance().streamingRecord(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ClientObserver<EmptyBean>() {
                        @Override
                        protected void onSuccess(EmptyBean content) {
                            initPlayerInfo();
                        }

                        @Override
                        protected void onResponseError(int errorCode, String errorInfo) {
                            initPlayerInfo();
                        }

                        @Override
                        protected void onOtherError(String error) {
                            initPlayerInfo();
                        }
                    });
        }
    }
}
